package com.duowan.realtime.server.util;

import java.util.Arrays;

public class HashUtils {
	/**
	 * 32位FNV算法
	 * @see http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
	 * @param hashByte the byte array to be hashed
	 * @return the 32 bit integer hash value
	 */
	public static int hashBytes(byte hashByte[]) {
		// 32 bit FNV constants. Using longs as Java does not support unsigned
		// datatypes.
		final long FNV_PRIME = 16777619;
		final long FNV_OFFSET_BASIS = 2166136261l;

		if (hashByte == null){
			return 0;
		}
		long result = FNV_OFFSET_BASIS;
		for (byte element : hashByte) {
			result = (result * FNV_PRIME) & 0xFFFFFFFF;
			result ^= element;
		}

		// return Arrays.hashCode(a);
		return (int) result;
	}
}
